package models

import (
	"time"
	"xorm.io/builder"
)

type PayStudent struct {
	Id           int       `xorm:"not null pk autoincr INT(11)"`
	SchoolId     int       `xorm:"INT(11)"`
	Paymoney     float64   `xorm:"FLOAT(11,1)"`
	Studentnum   int64     `xorm:"INT(11)"`
	Signstumoney float64   `xorm:"FLOAT(11,1)"`
	Yhzh         string    `xorm:"VARCHAR(50)"`
	Yhkh         string    `xorm:"VARCHAR(100)"`
	Compain      string    `xorm:"VARCHAR(100)"`
	Description  string    `xorm:"VARCHAR(120)"`
	OrderT       string    `xorm:"VARCHAR(50)"`
	Addtime      time.Time `xorm:"comment('下单日期') DATETIME"`
	Content      string    `xorm:"TEXT"`
	Paystatus    string    `xorm:"comment('缴费状态 0未缴费 1已缴费') ENUM('0','1')"`
	Submit       string    `xorm:"default '0' comment('是否已确认  0未确认 1已确认') ENUM('0','1')"`
	Paytype      string    `xorm:"comment('支付方式0对公账号 1 对私账号  2 支付宝 3微信') ENUM('0','1','2','3')"`
	Wxurl        string    `xorm:"VARCHAR(250)"`
	OrderNumber  string    `xorm:"comment('订单号') VARCHAR(50)"`
	Isused       string    `xorm:"default '1' ENUM('0','1')"`
	Is_invoice   string    `xorm:"default '' ENUM('0','1','2')"`
	UpdateTime   time.Time `xorm:"default 'CURRENT_TIMESTAMP' TIMESTAMP"`
}

type PayStudentList struct {
	Id           int64     `json:"id"`
	Name         string    `json:"name"`
	Name_short   string    `json:"name_short"`
	Studentnum   int64     `json:"studentnum"`
	Signstumoney float32   `json:"signstumoney"`
	Paystatus    string    `json:"paystatus"`
	Submit       string    `json:"submit"`
	Paytype      string    `json:"paytype"`
	Isused       string    `json:"isused"`
	Order_t      string    `json:"order_t"`
	Paymoney     float32   `json:"paymoney"`
	OrderNumber  string    `json:"order_number"`
	Addtime      time.Time `json:"addtime"`
	Content      string    `json:"content"`
	Wxurl        string    `json:"wxurl"`
	IsInvoice    string    `json:"is_invoice"`
}

func (c *PayStudent) GetPayStudentList(filter builder.Cond, pagesize, offset int) (list []*PayStudentList, count int64, err error) {
	err = engine.Table("dts_pay_student").Alias("pt").
		Select("pt.id,sl.name,sl.name_short,pt.studentnum,pt.signstumoney,pt.paymoney,pt.addtime,pt.order_number,pt.paytype,pt.paystatus,pt.submit,pt.isused,pt.content,pt.wxurl,pt.is_invoice").
		Join("left", []string{"dts_school", "sl"}, "sl.id = pt.school_id").
		Where(" pt.description = ? and pt.isused = ?", "购买学员", "1").
		And(filter).
		Limit(pagesize, offset).
		Desc("pt.addtime").
		Find(&list)
	count, _ = engine.Table("dts_pay_student").Alias("pt").
		Join("left", []string{"dts_school", "sl"}, "sl.id = pt.school_id").
		Where(" pt.description = ? and pt.isused = ?", "购买学员", "1").
		And(filter).
		Count()
	return list, count, err
}

func (c *PayStudent) GetOnePayStudent(id string) (has bool, list PayStudent, err error) {
	has, err = engine.Table("dts_pay_student").
		Where("id = ?", id).
		Get(&list)
	return has, list, err
}

func (c *PayStudent) Update(fields ...string) bool {
	var (
		err error
	)
	_, err = engine.Table("dts_pay_student").Id(c.Id).Cols(fields...).Update(c)
	//log.Print(err)
	if err != nil {
		return false
	}
	return true
}
